<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Withdraw;
use Pay;
use App\Models\User;
use Illuminate\Support\Facades\Redirect;

class WithdrawController extends Controller
{
	/**
	 * [withdrawLogs 所有提现记录]
	 * @Author   WHJ
	 * @DateTime 2019-10-29T21:51:44+0800
	 * @return   [view视图]                   [description]
	 */
    public function withdrawLogs()
    {
    	$list = Withdraw::orderBy('created_at','desc')->paginate(10);
    	return view('admin.pay.withdrawLogs',compact('list'));
    }

    public function getLogs(Request $request)
    {
        $page=$request->get('page');
        $size=$request->get('size');
        $list = Withdraw::orderBy('created_at','desc')->with("User")->paginate($size);
        return ['err'=>0,'data'=>$list];
    }

    public function examine(Request $request){
        $id=$request->get('id');
        $item=Withdraw::find($id);
        $item->state=1;
        $item->save();
        return ['err'=>0,'data'=>$item];
    }

    /**
     * [payForManager 返现给用户]
     * @Author   WHJ
     * @DateTime 2019-10-29T21:53:47+0800
     * @param    [int]                   $wid [提现id]
     * @return   [type]                        [description]
     */
    public function payForManager($wid)
    {
    	$withdraw = Withdraw::find($wid);
        if ($withdraw->state == 1) {
            return back()->with('msg','此提现请求已转账');
        }
        $user = User::find($withdraw->user_id);
    	// 转账测试
    	// return $this->aliPayTransfer(time(),'1246689685@qq.com',0.01,'魔兽怀旧服','提现到账','ALIPAY_LOGONID');
        $order_num = $withdraw->order_num;
        $account = $user->alipay_account;
        $money = $withdraw->money;
        $payer_show_name = '魔兽经典怀旧服-Trinity研究院';
        $remark = '提现到账';
        $data = $this->aliPayTransfer($order_num,$account,0.1,$payer_show_name,$remark);
        $data = json_decode($data);
        if ($data->code == 10000 && $data->msg == 'Success') {
            $withdraw->state = 1;
            $withdraw->save();
            return redirect('/admin/withdrawLogs')->with('msg','转账成功');
        } else {
            $withdraw->state == -1;
            $withdraw->save();
            return redirect('/admin/withdrawLogs')->with('msg','转账失败');
        }
    }

    /**
     * [aliPayTransfer 发起支付宝转账]
     * @Author   WHJ
     * @DateTime 2019-10-29T21:57:25+0800
     * @param    [type]                   $order_num       [订单号]
     * @param    [type]                   $account         [收款人支付宝账号]
     * @param    [type]                   $money           [转账金额]
     * @param    string                   $payer_show_name [付款方姓名]
     * @param    string                   $remark          [转账备注]
     * @param    string                   $payee_type      [收款方账户类型(ALIPAY_LOGONID | ALIPAY_USERID)]
     * @return   [type]                                    [description]
     */
    public function aliPayTransfer($order_num,$account,$money,$payer_show_name='',$remark='',$payee_type='ALIPAY_LOGONID')
    {
    	$config = [
		    'out_biz_no' => $order_num,                      // 订单号
		    'payee_type' => $payee_type,        // 收款方账户类型(ALIPAY_LOGONID | ALIPAY_USERID)
		    'payee_account' => $account,   // 收款方账户
		    'amount' => $money,                        // 转账金额
		    'payer_show_name' => $payer_show_name,             // 付款方姓名
		    'remark' => $remark,                      // 转账备注
		];
		return Pay::alipay()->transfer($config);
    }
}
